package com.itheima.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.Menu;
import com.itheima.mapper.MenuMapper;
import com.itheima.service.IMenuService;
import com.itheima.vo.MenuVo;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 后台菜单表 服务实现类
 * </p>
 *
 * @author fukeke
 * @since 2022-11-11
 */
@DubboService
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {
    @Autowired
    MenuMapper menuMapper;

    @Override
    public List<Menu> findAll() {

        QueryWrapper<Menu> qw=new QueryWrapper<>();

        return menuMapper.selectList(qw);
    }

    @Override
    public Menu findById(Long menuId) {
        return menuMapper.selectById(menuId);
    }

    @Override
    public List<MenuVo> findTreeList() {

        //首先查询一级目录（level=0)
        LambdaQueryWrapper<Menu> qw=new LambdaQueryWrapper<>();
        qw.eq(Menu::getLevel,0);

        //一级菜单目录集合
        List<Menu> menus = menuMapper.selectList(qw);
        //遍历一级目录，查询二级目录，并封装进MenuVo集合中
        List<MenuVo> menuVoList=new ArrayList<>();
        menus.forEach(menu -> {
            MenuVo menuVo=new MenuVo();
            BeanUtil.copyProperties(menu,menuVo);
            //获取一级菜单id，查询二级菜单
            LambdaQueryWrapper<Menu> qw2=new LambdaQueryWrapper<>();
            qw2.eq(Menu::getParentId,menu.getId());
            List<Menu> menus1 = menuMapper.selectList(qw2);

            //遍历二级菜单，封装成MenuVo对象，存入集合
            List<MenuVo> voList=new ArrayList<>();
            menus1.forEach(menu1 -> {
                MenuVo menuVo1=new MenuVo();
                BeanUtil.copyProperties(menu1,menuVo1);
                //设置二级菜单对象的children属性
                menuVo1.setChildren(new ArrayList<>());
                //将二级菜单MenuVo对象存入集合
                voList.add(menuVo1);
            });

            //设置一级菜单对象的children属性
            menuVo.setChildren(voList);
            //将一级菜单MenuVo对象存入集合
            menuVoList.add(menuVo);

        });
        return menuVoList;
    }

    @Override
    public PageBeanVo findByParentId(Long parentId, Integer pageNum, Integer pageSize) {
        //添加父级目录id作为条件
        LambdaQueryWrapper<Menu> qw=new LambdaQueryWrapper<>();
        qw.eq(Menu::getParentId,parentId);

        //添加分页条件
        Page<Menu> page=new Page<>(pageNum,pageSize);

        menuMapper.selectPage(page,qw);

        //将数据封装进PageBeanVo对象中
        return new PageBeanVo(pageNum,pageSize,page.getTotal(),page.getRecords());
    }

    @Override
    public void saveMenu(Menu menu) {
        menuMapper.insert(menu);
    }

    @Override
    public Boolean deleteMenu(Long id) {
        //判断此菜单下是否有子菜单
        LambdaQueryWrapper<Menu> qw=new LambdaQueryWrapper<>();
        qw.eq(Menu::getParentId,id);
        List<Menu> menus = menuMapper.selectList(qw);
        if(menus.size()==0) {//若无子菜单直接删除
            menuMapper.deleteById(id);
            return true;
        }else {
            return false;
        }
    }

    @Override
    public void updateMenu(Menu menu) {
        menuMapper.updateById(menu);
    }
}
